<template>
  <transition name="modal-fade">
    <div v-if="isVisible" class="modal-overlay" @click.self="handleOverlayClick">
      <div :class="[`modal-${type}`]" :style="modalStyle" class="modal-content">
        <div class="modal-status-bar"></div>
        <div v-if="showHeader" class="modal-header">
          <h3 class="modal-title">{{ title }}</h3>
          <button v-if="showClose" class="modal-close" @click="close">&times;</button>
        </div>
        <div class="modal-body">
          <div v-if="content" v-html="content"></div>
          <slot v-else></slot>
        </div>
        <div v-if="showFooter" class="modal-footer">
          <slot name="footer">
            <button v-if="showCancel" class="modal-btn modal-btn-cancel" @click="cancel">{{ cancelText }}</button>
            <button v-if="showConfirm" class="modal-btn modal-btn-confirm" @click="confirm">{{ confirmText }}</button>
          </slot>
        </div>
      </div>
    </div>
  </transition>
</template>

<script>
export default {
  name: 'ModernModal',
  props: {
    title: {
      type: String,
      default: '提示'
    },
    content: {
      type: String,
      default: ''
    },
    width: {
      type: [String, Number],
      default: '90%'
    },
    maxWidth: {
      type: [String, Number],
      default: '480px'
    },
    showHeader: {
      type: Boolean,
      default: true
    },
    showFooter: {
      type: Boolean,
      default: true
    },
    showClose: {
      type: Boolean,
      default: true
    },
    showCancel: {
      type: Boolean,
      default: true
    },
    showConfirm: {
      type: Boolean,
      default: true
    },
    cancelText: {
      type: String,
      default: '取消'
    },
    confirmText: {
      type: String,
      default: '确定'
    },
    closeOnClickOverlay: {
      type: Boolean,
      default: true
    },
    type: {
      type: String,
      default: 'default', // default, success, warning, error
      validator: (value) => ['default', 'success', 'warning', 'error'].includes(value)
    },
    theme: {
      type: String,
      default: 'light', // light, dark
      validator: (value) => ['light', 'dark'].includes(value)
    }
  },
  data() {
    return {
      isVisible: false,
    };
  },
  computed: {
    modalStyle() {
      return {
        width: typeof this.width === 'number' ? `${this.width}px` : this.width,
        maxWidth: typeof this.maxWidth === 'number' ? `${this.maxWidth}px` : this.maxWidth
      };
    }
  },
  methods: {
    open() {
      this.isVisible = true;
      document.body.style.overflow = 'hidden';
      this.$emit('open');
    },
    close() {
      this.isVisible = false;
      document.body.style.overflow = '';
      this.$emit('close');
    },
    cancel() {
      this.$emit('cancel');
      this.close();
    },
    confirm() {
      this.$emit('confirm');
      this.close();
    },
    handleOverlayClick() {
      if (this.closeOnClickOverlay) {
        this.close();
      }
    }
  },
  beforeDestroy() {
    document.body.style.overflow = '';
  }
};
</script>

<style scoped>

.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(3px);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 9999;
  padding: 16px;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
}

.modal-content {
  background-color: #ffffff;
  border-radius: 4px;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.12), 0 0 1px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  animation: modal-pop 0.3s cubic-bezier(0.16, 1, 0.3, 1);
  letter-spacing: -0.01em;
  max-height: calc(100vh - 32px);
  display: flex;
  flex-direction: column;
  position: relative;
}

/* 状态条样式 */
.modal-status-bar {
  height: 5px;
  width: 100%;
  border-top-left-radius: 3px;
  border-top-right-radius: 3px;
  background-color: #e5e7eb; /* 默认颜色 */
}

.modal-success .modal-status-bar {
  background-color: #10b981; /* 成功颜色 - 绿色 */
}

.modal-warning .modal-status-bar {
  background-color: #f59e0b; /* 警告颜色 - 黄色 */
}

.modal-error .modal-status-bar {
  background-color: #ef4444; /* 错误颜色 - 红色 */
}

.modal-header {
  padding: 10px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #f0f0f0;
}

.modal-title {
  margin: 0;
  font-size: 14px; /* 标题字体改为14px */
  font-weight: 600;
  color: #111827;
  letter-spacing: 1px;
}

.modal-close {
  background: none;
  border: none;
  color: #6b7280;
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s;
  padding: 0;
  font-size: 14px;
  line-height: 1;
  border-radius: 4px;
}

.modal-close:hover {
  background-color: rgba(0, 0, 0, 0.05);
  color: #111827;
}

.modal-body {
  padding: 16px 20px;
  color: #374151;
  font-size: 13px;
  line-height: 1.5;
  font-weight: 400;
  overflow-y: auto;
  flex: 1;
}

.modal-footer {
  padding: 12px 20px;
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  border-top: 1px solid #f0f0f0;
  background-color: #f9fafb;
}

.modal-btn {
  flex: 1;
  /*min-width: 80px;  缩小按钮宽度 */
  padding: 5px 10px; /* 缩小按钮内边距 */
  border-radius: 3px;
  font-size: 12px; /* 按钮字体改为12px */
  font-weight: 100;
  cursor: pointer;
  transition: all 0.2s;
  border: none;
  outline: none;
  letter-spacing: -0.01em;
}

.modal-btn-cancel {
  background-color: #ffffff;
  color: #374151;
  border: 1px solid #d1d5db;
}

.modal-btn-cancel:hover {
  background-color: #f9fafb;
  border-color: #9ca3af;
}

.modal-btn-confirm {
  background-color: #3b82f6;
  color: white;
  border: 1px solid transparent;
}

.modal-btn-confirm:hover {
  background-color: #2563eb;
}

/* 根据类型设置确认按钮颜色 */
.modal-success .modal-btn-confirm {
  background-color: #10b981;
}

.modal-success .modal-btn-confirm:hover {
  background-color: #059669;
}

.modal-warning .modal-btn-confirm {
  background-color: #f59e0b;
}

.modal-warning .modal-btn-confirm:hover {
  background-color: #d97706;
}

.modal-error .modal-btn-confirm {
  background-color: #ef4444;
}

.modal-error .modal-btn-confirm:hover {
  background-color: #dc2626;
}

/* 暗色主题 */
.modal-overlay[data-theme="dark"] .modal-content {
  background-color: #1f2937;
  border: 1px solid #374151;
}

.modal-overlay[data-theme="dark"] .modal-header {
  border-bottom-color: #374151;
}

.modal-overlay[data-theme="dark"] .modal-title {
  color: #f9fafb;
}

.modal-overlay[data-theme="dark"] .modal-close {
  color: #9ca3af;
}

.modal-overlay[data-theme="dark"] .modal-close:hover {
  background-color: rgba(255, 255, 255, 0.1);
  color: #f9fafb;
}

.modal-overlay[data-theme="dark"] .modal-body {
  color: #d1d5db;
}

.modal-overlay[data-theme="dark"] .modal-footer {
  background-color: #111827;
  border-top-color: #374151;
}

.modal-overlay[data-theme="dark"] .modal-btn-cancel {
  background-color: #374151;
  color: #f9fafb;
  border-color: #4b5563;
}

.modal-overlay[data-theme="dark"] .modal-btn-cancel:hover {
  background-color: #4b5563;
  border-color: #6b7280;
}

/* 过渡动画 */
.modal-fade-enter-active, .modal-fade-leave-active {
  transition: opacity 0.3s;
}

.modal-fade-enter, .modal-fade-leave-to {
  opacity: 0;
}

@keyframes modal-pop {
  0% {
    transform: scale(0.95);
    opacity: 0;
  }
  100% {
    transform: scale(1);
    opacity: 1;
  }
}

@media (max-width: 480px) {
  .modal-content {
    width: 100%;
  }

  .modal-header {
    padding: 14px 16px;
  }

  .modal-body {
    padding: 14px 16px;
  }

  .modal-footer {
    padding: 10px 16px;
  }

  .modal-btn {
    flex: 1;
    min-width: 0;
  }
}
</style>
